package com.mx.recycle_adapt.activity

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import com.mx.adapt.MXBaseSimpleAdapt
import com.mx.recycle_adapt.R
import com.mx.recycle_adapt.databinding.AdaptSimpleIndexBinding
import kotlinx.android.synthetic.main.activity_any_type_recycle_view.*

class IndexedActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_any_type_recycle_view)
        recycleView.layoutManager = LinearLayoutManager(this)
        val adapt = SpanCountAdapt()
        recycleView.adapter = adapt
        adapt.list.addAll((0 until 1000000))
        adapt.notifyDataSetChanged()
    }


    private class SpanCountAdapt : MXBaseSimpleAdapt<Int>() {
        init {
            setItemClick { position, record ->
                list.removeAt(position)
                notifyItemRemoved(position)
            }
        }

        override fun createItem(
            inflater: LayoutInflater,
            parent: ViewGroup,
            viewType: Int
        ): AdaptSimpleIndexBinding {
            return AdaptSimpleIndexBinding.inflate(inflater, parent, false)
        }

        override fun bindView(position: Int, binding: ViewBinding, record: Int) {
            binding as AdaptSimpleIndexBinding
            binding.indexTxv.text = "" + (record + 1)
        }
    }
}